package com.jianbing.zuixia.controller;

import java.time.LocalDate;
import java.time.temporal.WeekFields;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.jianbing.common.core.domain.model.LoginUser;
import com.jianbing.common.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.jianbing.common.annotation.Log;
import com.jianbing.common.core.controller.BaseController;
import com.jianbing.common.core.domain.AjaxResult;
import com.jianbing.common.enums.BusinessType;
import com.jianbing.zuixia.domain.ZuixiaCheckinRecord;
import com.jianbing.zuixia.service.IZuixiaCheckinRecordService;
import com.jianbing.common.utils.poi.ExcelUtil;
import com.jianbing.common.core.page.TableDataInfo;

/**
 * zuixia签到记录Controller
 *
 * @author windbless
 * @date 2024-06-18
 */
@RestController
@RequestMapping("/zuixia/qiandao")
public class ZuixiaCheckinRecordController extends BaseController {
    @Autowired
    private IZuixiaCheckinRecordService zuixiaCheckinRecordService;


    public static long getWeekNumberForToday() {
        LocalDate today = LocalDate.now();
        return today.get(WeekFields.ISO.weekOfWeekBasedYear());
    }

    @GetMapping("/qiandao")
    public TableDataInfo qiandao() {
        // 获取当前登录用户的系统用户ID
        Long systemUserId = getappid(SecurityUtils.getLoginUser());
        String systemUserNickname = SecurityUtils.getLoginUser().getUser().getNickName();
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date()); // 设置为当前时间
        calendar.set(Calendar.HOUR_OF_DAY, 0); // 小时设为0
        calendar.set(Calendar.MINUTE, 0); // 分钟设为0
        calendar.set(Calendar.SECOND, 0); // 秒设为0
        calendar.set(Calendar.MILLISECOND, 0); // 毫秒设为0

        Date todayStart = calendar.getTime(); // 获取今天的 00:00:00 时间点

        // 创建一个新的 ZuixiaCheckinRecord 实例并设置筛选条件
        ZuixiaCheckinRecord filter = new ZuixiaCheckinRecord();
        filter.setUserId(systemUserId);
        filter.setCheckinTime(todayStart);

        ZuixiaCheckinRecord totalfilter = new ZuixiaCheckinRecord();
        totalfilter.setUserId(systemUserId);
        List<ZuixiaCheckinRecord> usertotal = zuixiaCheckinRecordService.selectZuixiaCheckinRecordList(totalfilter);

        ZuixiaCheckinRecord totalweeksign = new ZuixiaCheckinRecord();
        filter.setCurrentWeek(getWeekNumberForToday());
        List<ZuixiaCheckinRecord> thisweektotal = zuixiaCheckinRecordService.selectZuixiaCheckinRecordList(totalfilter);

        // 根据筛选条件查询
        List<ZuixiaCheckinRecord> list = zuixiaCheckinRecordService.selectZuixiaCheckinRecordList(filter);

        if (list.size() > 0) {
            TableDataInfo test = new TableDataInfo();
            test.setCode(500);
            test.setMsg("今天" + systemUserNickname + "已签到");
            test.setRows(list);
            return test;
        } else {
            ZuixiaCheckinRecord zuixiaCheckinRecord = new ZuixiaCheckinRecord();
            zuixiaCheckinRecord.setUserId(systemUserId);
            zuixiaCheckinRecord.setCheckinTime(new Date());
            zuixiaCheckinRecord.setCurrentCheckinDays((long) (usertotal.size() + 1));
            zuixiaCheckinRecord.setCurrentWeek(getWeekNumberForToday());
            if (thisweektotal.size() > 0) {
                zuixiaCheckinRecord.setWeeklyCheckinCount(thisweektotal.get(0).getWeeklyCheckinCount() + 1);
            } else {
                zuixiaCheckinRecord.setWeeklyCheckinCount(1L);
            }
            zuixiaCheckinRecordService.insertZuixiaCheckinRecord(zuixiaCheckinRecord);
            TableDataInfo test = new TableDataInfo();
            test.setCode(200);
            test.setMsg("签到成功");
            List<ZuixiaCheckinRecord> listnow = zuixiaCheckinRecordService.selectZuixiaCheckinRecordList(filter);
            test.setRows(listnow);
            return test;
        }

    }


    private Long getappid(LoginUser loginUser) {
        return loginUser.getUserId();
    }

    /**
     * 查询zuixia签到记录列表
     */

    @GetMapping("/list")
    public TableDataInfo list(ZuixiaCheckinRecord zuixiaCheckinRecord) {
        startPage();
        List<ZuixiaCheckinRecord> list = zuixiaCheckinRecordService.selectZuixiaCheckinRecordList(zuixiaCheckinRecord);
        return getDataTable(list);
    }

    /**
     * 导出zuixia签到记录列表
     */

    @Log(title = "zuixia签到记录", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ZuixiaCheckinRecord zuixiaCheckinRecord) {
        List<ZuixiaCheckinRecord> list = zuixiaCheckinRecordService.selectZuixiaCheckinRecordList(zuixiaCheckinRecord);
        ExcelUtil<ZuixiaCheckinRecord> util = new ExcelUtil<ZuixiaCheckinRecord>(ZuixiaCheckinRecord.class);
        util.exportExcel(response, list, "zuixia签到记录数据");
    }

    /**
     * 获取zuixia签到记录详细信息
     */

    @GetMapping(value = "/{checkinId}")
    public AjaxResult getInfo(@PathVariable("checkinId") Long checkinId) {
        return success(zuixiaCheckinRecordService.selectZuixiaCheckinRecordByCheckinId(checkinId));
    }

    /**
     * 新增zuixia签到记录
     */

    @Log(title = "zuixia签到记录", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ZuixiaCheckinRecord zuixiaCheckinRecord) {
        return toAjax(zuixiaCheckinRecordService.insertZuixiaCheckinRecord(zuixiaCheckinRecord));
    }

    /**
     * 修改zuixia签到记录
     */

    @Log(title = "zuixia签到记录", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ZuixiaCheckinRecord zuixiaCheckinRecord) {
        return toAjax(zuixiaCheckinRecordService.updateZuixiaCheckinRecord(zuixiaCheckinRecord));
    }

    /**
     * 删除zuixia签到记录
     */

    @Log(title = "zuixia签到记录", businessType = BusinessType.DELETE)
    @DeleteMapping("/{checkinIds}")
    public AjaxResult remove(@PathVariable Long[] checkinIds) {
        return toAjax(zuixiaCheckinRecordService.deleteZuixiaCheckinRecordByCheckinIds(checkinIds));
    }
}
